package com.cjh.utils;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class ResultUtil {

    /**
     * 打印流返回json
     * @param response
     * @param result
     */
    public static void returnJson(HttpServletResponse response, Object result) {

        PrintWriter writer = null;
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        try {
            writer = response.getWriter();
            String json = JSONObject.toJSONString(result);
            writer.print(json);
        } catch (IOException e) {
            System.err.println(e);
        } catch (Exception e) {
            System.err.println(e);
        } finally {
            if (writer != null)
                writer.flush();
                writer.close();
        }

    }
}